//实现输出的式子带有括号、括号的位置正确
package ProduceFormula;

import java.util.Random;

public class formula {
    Random randnum = new Random();

    public String[] Formulanobracket() {
        String s = "";
        String[] str;
        char[] a = {'+', '-', '*', '÷'};
        //构造一个randomNum类的对象,用来调用其中的getNum方法，该方法用来产生一个随机数
        int length = randnum.nextInt(7) + 2;
        for (int i = 1; i <= length; i++) {
            int number = randnum.nextInt(100);
            if (number>50) {
                s = s + fraction.makeFenshu() + " ";
            }
            else {
                s = s + number + " ";
            }
            if (i < length) {
                char x = a[randnum.nextInt(4)];
                s = s + x + " ";
            }
        }
        str = s.split(" ");//字符串转化为字符串数组,以空格作为分隔符
        return str;
    }

    public String Bracket(String str[]) {
        String s2 = "";
        int countLBracket = 0;//记录产生的左括号的个数
        boolean choicechar;//选择是否要生成括号
        for (int i = 0; i < str.length; i = i + 2) {
            choicechar = randnum.nextBoolean();
            if (choicechar && i != str.length - 1) { //产生左括号
                str[i] = "( " + str[i];
                countLBracket++;
            }
            choicechar = randnum.nextBoolean();
            if (choicechar && !str[i].startsWith("(") && countLBracket != 0) {
                str[i] = str[i] + " )";
                countLBracket--;
            }
        }
        for (int i = countLBracket; i > 0; i--) {
            str[str.length - 1] = str[str.length - 1] + " )";
        }
        for (String i : str) {
            s2 = s2 + i + " ";
        }

        return s2;
    }
}